import os
from common.handlerequest import SendRequest
from common.readexcel import ReadExcel
from common.handlepath import DATADIR
import jsonpath
from common.handleconfig import conf
from common.handledata import CaseData, replace_data

from common.connectdb import DB

request = SendRequest()
url = conf.get("evn","url")+ "/user/login/"
print(url)
date = {"username": "xiaoyu","password": "111"}
response = request.send_request(url=url, method="post",json=date)  # 发送请求
print(response.json())
"""

base_url = "http://api.lemonban.com/futureloan"
case_file = os.path.join(DATADIR, "apicases.xlsx")
excel = ReadExcel(case_file, "register")

cases = excel.read_data()
method = cases[0]["method"]
# print(method)
url = base_url + cases[0]["url"]
# print(url)
data = eval(cases[0]["data"])
print(data)
CaseData.username = generate_data("yu")
print(CaseData.username)
CaseData.email = generate_data("yu", "@qq.com")
print(CaseData.email)
data = eval(replace_data(cases[0]["data"]))  # 替换用例的数据

print(data)
expected = eval(cases[0]["expected"])
print(expected)
if expected =={}:
    print(111)
else:
    print(222)


headers = {"X-Lemonban-Media-Type": "lemonban.v2"}
req = SendRequest()
res = req.send_request(url=url, method=method, json=data, headers=headers)
case= res.json()

print(case)
member_id = jsonpath.jsonpath(case, "$..id")[0]
print(member_id)

"""

"""
#注册
request=SendRequest()
url = conf.get("evn", "url") + "/member/register"
headers = eval(conf.get("evn", "headers"))
data = {"mobile_phone":15912345677,"pwd":"admin1234","reg_name":"xioaoyu_01","type":"0"}
response = request.send_request(url=url, headers=headers, json=data, method="post")  # 发送请求
print(response.json())

"""

"""
#普通用户登录

request=SendRequest()
url = conf.get("evn", "url") + "/member/login"
headers = eval(conf.get("evn", "headers"))
login_data = {
    "mobile_phone": conf.get("test_data", "mobile_phone"),
    "pwd": conf.get("test_data", "pwd")
    }
response = request.send_request(url=url, headers=headers, json=login_data, method="post")  # 发送请求
print(response.json())
member_id = jsonpath.jsonpath(response.json(), "$..id")[0]
token = jsonpath.jsonpath(response.json(), "$..token_type")[0] + " " + \
                    jsonpath.jsonpath(response.json(), "$..token")[0]
print(member_id)
print(token)
"""

"""
#管理员登录

request=SendRequest()
url = conf.get("evn", "url") + "/member/login"
headers = eval(conf.get("evn", "headers"))
login_data = {
    "mobile_phone": conf.get("test_data", "admin_phone"),
    "pwd": conf.get("test_data", "admin_pwd")
    }
response = request.send_request(url=url, headers=headers, json=login_data, method="post")  # 发送请求
print(response.json())
member_id = jsonpath.jsonpath(response.json(), "$..id")[0]
token = jsonpath.jsonpath(response.json(), "$..token_type")[0] + " " + \
                    jsonpath.jsonpath(response.json(), "$..token")[0]
print(member_id)
print(token)
"""

"""修改用户信息
request=SendRequest()
url = conf.get("evn", "url") + "/member/update"
headers = eval(conf.get("evn", "headers"))
headers["Authorization"] =token
data = {"member_id":member_id,"reg_name":"xioayu_01"}
response = request.send_request(url=url, headers=headers, json=data, method="patch")  # 发送请求
print(response.json())
"""

"""获取会员信息
request=SendRequest()
url = conf.get("evn", "url") + "/member/708/info"
headers = eval(conf.get("evn", "headers"))
headers["Authorization"] =token
response = request.send_request(url=url, headers=headers, method="get")  # 发送请求
print(response.json())
"""

"""
#充值

request=SendRequest()
recharge_url = conf.get("evn", "url") + "/member/recharge"
headers = eval(conf.get("evn", "headers"))
recharge_data = {"member_id":member_id,"amount":"50000"}
headers["Authorization"] =token
recharge_response = request.send_request(url=recharge_url, headers=headers, json=recharge_data, method="post")  # 发送请求
print(recharge_response.json())

"""

"""添加项目
request=SendRequest()
recharge_url = conf.get("evn", "url") + "/loan/add"
headers = eval(conf.get("evn", "headers"))
recharge_data = {"member_id":member_id,"title":"水仙花种植项目02","amount":9000.00,"loan_rate":8.0,"loan_term":3,"loan_date_type":1,"bidding_days":7}
headers["Authorization"] =token
recharge_response = request.send_request(url=recharge_url, headers=headers, json=recharge_data, method="post")  # 发送请求
print(recharge_response.json())
"""

"""评审项目
request=SendRequest()
recharge_url = conf.get("evn", "url") + "/loan/audit"
headers = eval(conf.get("evn", "headers"))
recharge_data = {"loan_id":1017,"approved_or_not":"false"}
headers["Authorization"] =token
recharge_response = request.send_request(url=recharge_url, headers=headers, json=recharge_data, method="patch")  # 发送请求
print(recharge_response.json())
"""

"""分页获取项目列表
request=SendRequest()
recharge_url = conf.get("evn", "url") + "/loans"
headers = eval(conf.get("evn", "headers"))
data = {"pageIndex":1,"pageSize":500}
# headers["Authorization"] =token
recharge_response = request.send_request(url=recharge_url, headers=headers, json=data, method="get")  # 发送请求
print(recharge_response.json())

"""
